package com.example.network.JSON_parsing;

import com.example.network.entity.Person;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JSON_GSON {
    private static Gson gson = new Gson();

    /**
     * 解析对象
     */
    public static Person AnalyticalObject(String json){
        Person person = gson.fromJson(json, Person.class);

        return person;
    }
    /**
     * 解析对象数组
     */
    public static ArrayList<Person> AnalyticalArrayObject(String json){
        ArrayList<Person> people = gson.fromJson(json, new TypeToken<List<Person>>() {
        }.getType());
        return people;
    }
    public static Map<String,Person> AnalyticalMapObject(String json){
        Map<String,Person> map = new HashMap<String,Person>();
        map = (Map<String,Person>) gson.fromJson(json, new TypeToken<Map<String,Person>>(){}.getType());

        return map;
    }


    public static String toJson(Object o){
        return gson.toJson(o);
    }
}
